//index.js
//获取应用实例
const app = getApp()


Page({
  globalData: {
    userInfo: {},
    hasUserInfo: false,
    userAddress: null,
    systemInfo:'',
    canvasHide:'',
    qrcode_temp:'',
  },
  data: {
    motto: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    personalScroe: '100'
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../personal/index'
    })
  },
  trainChoice: function () {
    wx.navigateTo({
      url: '../trainingChoice/index'
    })
  },
  waiting: function () {
    wx.navigateTo({
      url: '../answer/index'
    })
  },
  allLocation: function(){
    wx.navigateTo({
      url: '../location/index',
    })
  },
  getUserInfo: function (e) {
    wx.login({
      
    })
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  onLoad: function () {
    const that = this;
    this.setData({
      userInfo:getApp().globalData.userInfo,
      canvasHide: true,
    })
    if(this.data.userInfo == null){
      wx.redirectTo({
        url: '../Authorization/index',
      })
    }
    wx.request({
      url: getApp().globalData.url + '/access.do',
      method: "GET",
      data: {},
      header: {
        'content-type': 'json'
      },
      success: function (res) {
        that.setData({
          motto:res.data
        })
      },
      fail: function (res) {
        
      }
    })

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          systemInfo: res
        });
      }
    });

    
  },
  
  download:function(){
    const that = this;
    wx.downloadFile({
      url: getApp().globalData.url + '/image.png',
      success: function (res) {
        console.log('二维码：' + res.tempFilePath)
        //缓存二维码
        that.setData({
          qrcode_temp: res.tempFilePath
        })
        console.log('开始绘制图片');
        that.drew();
      }
    })
  },

  drew:function(){
    const ctx = wx.createCanvasContext('qrCanvas');
    const that = this;
    
    let width = that.data.systemInfo.windowWidth*0.7;
    let height = 300;
    ctx.drawImage('../images/shar.jpg', 0, 0, width+1, height)
    ctx.setFillStyle('white')//填充白色
    ctx.fillRect(0, height - 60, width, 60)//坐标x:0,y:height-60 宽高。。。
    ctx.moveTo(width / 2, 20)//画笔移动到垂直居中位置（高度20不重要，我随便写的）
    ctx.setTextAlign('center')//设置文字要垂直居中

    ctx.setFillStyle('#333')//字体颜色
    ctx.setFontSize(15)//字体大小
    ctx.fillText("社科答题小程序", width / 2 + 20, height - 25)//字体内容和位置
    ctx.drawImage(that.data.qrcode_temp, 10, height-55, 50, 50)
    ctx.draw()
    that.setData({
      canvasHide: false
    })
  },

  save:function(){
    wx.canvasToTempFilePath({
      canvasId: 'qrCanvas',
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        wx.previewImage({
          urls: [res.tempFilePath]
        })
      }
    })
  },

  hide:function(){
    const that = this;
    that.setData({
      canvasHide: true
    })
  },


  onShow: function () {
    wx.onSocketOpen(function (res) {
      console.log('开着的！')
      wx.closeSocket()
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '思政理论课知识答题小程序',
      path: '/pages/Authorization/index',
      imageUrl: '../images/shar.jpg'
    }
  },

  
})
